package com.sumainfor.pc.epc.service.impl;


import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.epc.dao.CommodityMapper;
import com.sumainfor.pc.epc.dao.DecorateComboMapper;
import com.sumainfor.pc.epc.dao.UtilsMapper;
import com.sumainfor.pc.epc.service.DecorateComboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Auther: zhlu
 * @Date: 2018/11/3 20:45
 * @Description:
 */
@Service
public class DecorateComboServiceImpl implements DecorateComboService{

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log= LoggerFactory.getLogger(DecorateComboServiceImpl.class);
    @Autowired
    private DecorateComboMapper decorateComboMapper;
    @Autowired
    private UtilsMapper utilsMapper;
    @Autowired
    private CommodityMapper commodityMapper;

    String UUID=null;
    public List<Map<String, Object>> getDecorateComboList(Map<String, Object> params) {
        return decorateComboMapper.getDecorateComboList(params);
    }


    public Integer getDecorateComboListCount(Map<String, Object> params) {
        return decorateComboMapper.getDecorateComboListCount(params);
    }

    /**
     * 新增装修套餐
     * @param params
     * @return
     */
    @Override
    public Integer addDecorateCombo(Map<String, Object> params) {
        Integer result=0;
        /**
         * 加锁操作
         */
        lock.lock();
        try{
            try {
                Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                    put("uuid", "CombId");
                    put("database", Constants.DATA_SOURCE_TCEPC);
                    put("surface", "tcepc_decorate_combo");
                }});
                if(uuid!=null){
                    int number=Integer.valueOf(uuid.get("uuid").toString());
                    UUID= ComUtils.getLocalTrmSeqNum("dc",number);
                }else{
                    UUID=ComUtils.getLocalTrmSeqNum("dc",0);
                }
                params.put("by", ContextUtils.getUserId());//操作人
                params.put("CombId",UUID);
                params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(params);
                result=decorateComboMapper.addDecorateCombo(params);
                if (result>0){
                    List<Map<String,Object>> list= (List<Map<String, Object>>) params.get("data");
                    for(int i=0;i<list.size();i++){
                        params.putAll(list.get(i));

                        System.out.println("params:"+params);
                        decorateComboMapper.addDecorateComboInfo(params);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }

    @Override
    public Integer addDecorateComboInfo(Map<String, Object> params) {
        return null;
    }
    /**
     * 修改装修套餐是否停用
     * @param params
     * @return
     */
    @Override
    public Integer updateDecorateCombo(Map<String, Object> params) {
        System.out.println("被调用了");
        int result=0;
        try {
            params.put("by", ContextUtils.getUserId());//操作人
            params.put("term", "3");// 更新端 Update端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(params);
            result=decorateComboMapper.updateDecorateCombo(params);
        }
        catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

    @Override
    public Integer updateAndInfo(Map<String, Object> params) {
        int result=0;
        try {
            params.put("by", ContextUtils.getUserId());//操作人
            params.put("term", "3");// 更新端 Update端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(params);
            result=decorateComboMapper.updateDecorateCombo(params);
            System.out.println("result===================:"+result);
            if (result>0){
                int count=decorateComboMapper.deleteDecorateComboByCombId(params);//根据装修套餐主表编号去删除详表
                if (count>0){
                    List<Map<String,Object>> list= (List<Map<String, Object>>) params.get("data");
                    for(int i=0;i<list.size();i++){
                        params.putAll(list.get(i));
                        System.out.println("params:"+params);
                        decorateComboMapper.addDecorateComboInfo(params);
                    }
                }else{//如果删除详表失败，就事务回滚
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

    /**
     * 编辑装修套餐的时候回显数据
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> getDecorateComboByCombId(Map<String, Object> params) {
        Map<String,Object> sjkmap=new HashMap<String,Object>();
        try {
            sjkmap=decorateComboMapper.getDecorateComboByCombId(params);
            System.out.print("装修套餐主表的map:"+sjkmap);
            List<Map<String,Object>> list=decorateComboMapper.getDecorateComboInfoByCombId(sjkmap);
            for (Map<String, Object> map : list) {
                System.out.println("for1111 map"+map);
                if(map.get("ComType")!=null){
                    map.put("goods",commodityMapper.getCommodityByComType(map));
                }
                if(map.get("ComName")!=null){
                    map.put("specifications", commodityMapper.getCommoditySpeByComName(map));
                }
                if(map.get("ComSpeName")!=null){
                    map.put("clist", commodityMapper.getCommoditySpeMoByComSpeName(map));
                }
                System.out.println("for map"+map);
            }
            sjkmap.put("list",list);
        }catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return sjkmap;
    }
}
